var rootPath = getRootPath();
var isRightPhoneNum = false;//是否是正确的手机号
var isRightEmail = false;
var click_flag = false;
var isPhone = isPhoneScreen();

$(function() {
	//1.手机/邮箱重新输入时(获取焦点),去除所有的错误控制信息
	$("#phoneEmail").focus(function() {
		hideMsg();
		isRightPhoneNum = false;
		isRightEmail = false;
		
		$("#sendCode").attr("disabled",true);
		$("#get_code").removeClass("active");
	});
	
	//2.手机号码/邮箱判断是否合法(移除焦点)
	$("#phoneEmail").blur(function(){
		if(isPhoneAvailable($(this).val()) == false && checkEmail($(this).val()) == false){
			showErrMsg("请输入有效的手机号/邮箱");
			$("#sendCode").css({"background-color":"#bbbbbb","cursor":"default"});
			$("#get_code").css({"color":"#bbbbbb","cursor":"default"});
			
			$("#sendCode").attr("disabled",true);
			$("#get_code").removeClass("active");
			return;
		}

		if(isPhoneAvailable($(this).val())) {
			$.ajax({
				type: "get",
		        url: rootPath + "/userFront/checkPhone",
		        data: {
		        	"phone": $("#phoneEmail").val()
		        },success: function (data){
		        	if("0000" != data.returnCode || "true" != data.returnObject) {
		        		showErrMsg("手机号未注册,请先注册");
		        		isRightPhoneNum = false;	        		
		        	} else {
		        		hideMsg();
		        		isRightPhoneNum = true;
		        		
		        	}
		        	if(isRightPhoneNum) {
		        		$("#sendCode").css({"background-color":"#EA9792","cursor":"pointer"});
		    			$("#get_code").css({"color":"#B4282D","cursor":"pointer"});
		    			
		    			$("#sendCode").attr("disabled",false);
		    			$("#get_code").addClass("active");
		        	} else {
		        		$("#sendCode").css({"background-color":"#bbbbbb","cursor":"default"});
		    			$("#get_code").css({"color":"#bbbbbb","cursor":"default"});
		    			
		    			$("#sendCode").attr("disabled",true);
		    			$("#get_code").removeClass("active");
		        	}
		        },error: function (jqXHR, textStatus, errorThrown) {
		        	layer.alert("error");
		        }
			});
		} 
		
		if(checkEmail($(this).val())) {
			$.ajax({
				type: "get",
		        url: rootPath + "/userFront/checkEmail",
		        data: {
		        	"email" : $("#phoneEmail").val()
		        },success: function (data){
		        	if("0000" != data.returnCode || "true" != data.returnObject) {
		        		showErrMsg("邮箱未注册,请先注册");
		        		isRightPhoneNum = false;	        		
		        	} else {
		        		hideMsg();
		        		isRightPhoneNum = true;
		        	}
		        	if(isRightPhoneNum) {
		        		$("#sendCode").css({"background-color":"#EA9792","cursor":"pointer"});
		    			$("#get_code").css({"color":"#B4282D","cursor":"pointer"});
		    			
		    			$("#sendCode").attr("disabled",false);
		    			$("#get_code").addClass("active");
		        	} else {
		        		$("#sendCode").css({"background-color":"#bbbbbb","cursor":"default"});
		    			$("#get_code").css({"color":"#bbbbbb","cursor":"default"});
		    			
		    			$("#sendCode").attr("disabled",true);
		    			$("#get_code").removeClass("active");
		        	}
		        },error: function (jqXHR, textStatus, errorThrown) {
		        	layer.alert("error");
		        }
			});
		}
	});
	
	
	
	//3.两个框都有值的情况，确定按钮显示正常
	$("#phoneEmail,#code").bind('input propertychange',function() {
		if($("#phoneEmail").val() == '' 
			|| $("#code").val() == ''
				|| $.trim($("#code").val()).length < 4
				|| !isRightPhoneNum) {
			$("#next").css({"background-color":"#bbbbbb","cursor":"default"});
			$("#next").attr("disabled",true);
			click_flag = false;
		} else {
			$("#next").css({"background-color":"rgb(197,24,10)","cursor":"pointer"});
			$("#next").attr("disabled",false);
			click_flag = true;
		}
	});
	
	//4.发送验证码之后，显示倒计时60秒。
	if(isPhone) {
		$("#get_code").click(function() {
			if($(this).hasClass("active")) {
				getCheckCode($(this));
			}
		});		
	} else {
		$("#sendCode").click(function() {
			event.stopPropagation();
			getCheckCode($(this));
		});		
	}
	
	//5.确定按钮
	$("#next").click(next);
});

function showErrMsg(msg){
	if(msg != ''){
		$("#phone_email_msg").text(msg);
		$(".msg").show();
	}
}

function hideMsg() {
	$("#phone_email_msg").text("");
	$(".msg").hide();
}

//获得验证码接口 
function getCheckCode(obj) {
	if($("#phoneEmail").val() == ''){
		showErrMsg("请输入手机号/邮箱");
		return;
	}
	
	if(isRightPhoneNum == false && isRightEmail == false){
		showErrMsg("请输入正确的手机号/邮箱")
		return;
	}
	
	//倒计时中不能发送
	var countdown = $("#get_code").attr("countdown");
	if(countdown != '' && countdown>0){
		return;
	}
	
	showErrMsg("正在发送,请稍等");
//	setTime($(this));
	setTime(obj);
	if(isPhoneAvailable($("#phoneEmail").val())) {
		//神策数据 埋点
		sensors.track('Adreambox_SmsGet', {
			current_business : '忘记密码',
			get_type : '手机'
		});
		$.ajax({
			type: "get",
		    url: rootPath + "/userFront/sendCodeSmsValidate",
		    data: { 
		    	"phone" : $("#phoneEmail").val(),
		    	"flag" : 1,
		    	"checkCode" : 'noNeedCheckCode'
		    },
		    dataType: "json",
		    success: function (data) {
		    	if("0000" == data.returnCode) {
		    		showErrMsg("发送成功");
		        } else {
		        	showErrMsg(data.returnMessage);
			    }
		    },
		    error:function (jqXHR, textStatus, errorThrown) {      
		    	showErrMsg("发送验证码失败,请检查您的网络.");
		    }
		});
	} else if(checkEmail($("#phoneEmail").val())) {
		//神策数据 埋点
		sensors.track('Adreambox_SmsGet', {
			current_business : '忘记密码',
			get_type : '邮箱'
		});
		$.ajax({
			type: "get",
		    url: rootPath + "/userFront/sendCodeEmailValidate",
		    data: { 
		    	"email" : "'" + $("#phoneEmail").val() + "'",
		    	"flag" : 1
		    },
		    dataType: "json",
		    success: function (data) {
		    	if("0000" == data.returnCode) {
		    		showErrMsg("发送成功");
		        } else {
		        	showErrMsg(data.returnMessage);
			    }
		    },
		    error:function (jqXHR, textStatus, errorThrown) {      
		    	showErrMsg("发送验证码失败,请检查您的网络.");
		    }
		});
	}
}

//验证码倒计时
function setTime(obj) {
	var countdown = obj.attr("countdown");
	if(countdown == undefined || countdown == '' || countdown == 0) {
		countdown = 60;
	}
	var interval = setInterval(function() {
		countdown--;
		obj.attr("countdown",countdown);
		if(countdown == 0){
			if(isPhone) {
				obj.css({"color":"#B4282D","cursor":"pointer"});
				obj.addClass("active");
				obj.text("获取验证码");
			} else {
				obj.css({"background-color":"#EA9792","cursor":"pointer"});
				obj.attr('disabled',false); 
				obj.val("获取验证码");
			}
			window.clearInterval(interval);
		} else {
			if(isPhone) {
				obj.css({"color":"#bbbbbb","cursor":"default"});
				obj.text("重新发送(" + countdown + ")");
				obj.removeClass("active");
			} else {
				obj.css({"background-color":"#bbbbbb","cursor":"default"});
				obj.val("重新发送(" + countdown + ")");
				obj.attr('disabled',true);
			}
		}
	}, 1000);
}

//下一步
function next() {
	if(!click_flag) return;
	var code = $("#code").val();
	var phoneEmail = $("#phoneEmail").val();
	if(code == '') {
		showErrMsg("验证码不能为空");
		return;
	}
	if(isPhoneAvailable($("#phoneEmail").val())) {
		showLoading();
		$.ajax({
			url: rootPath + '/userFront/verifyPhoneCode?code=' + code + '&phone=' + phoneEmail,
	        type: 'GET',
	        contentType: 'application/json;charset=UTF-8',
	        success: function (data) {
	        	hideLoading();
	        	if(data.returnCode == '0000') {
	        		if(!data.returnObject) {
	        			showErrMsg("输入的验证码有误");
	        		} else {
	        			parent.$("#content").prop("src","setNewPwd.html");
	        		} 
	        	} else {
	        		showErrMsg(data.returnMessage);
	    		}
	        },
	        error: function (jqXHR, textStatus, errorThrown) {
	        	hideLoading();
	        	layer.alert("error");
	        }
	    });
	} else {
		showLoading();
		$.ajax({
			url: rootPath + '/userFront/verifyEmailCode?code=' + code + '&email=' + phoneEmail,
	        type: 'GET',
	        contentType: 'application/json;charset=UTF-8',
	        success: function (data) {
	        	hideLoading();
	        	if(data.returnCode == '0000') {
	        		if(!data.returnObject) {
	        			showErrMsg("输入的验证码有误");
	        		} else {
	        			parent.$("#content").prop("src","setNewPwd.html");
	        		} 
	        	} else {
	        		showErrMsg(data.returnMessage);
	    		}
	        },
	        error: function (jqXHR, textStatus, errorThrown) {
	        	hideLoading();
	        	layer.alert("error");
	        }
	    });
	}
}